<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '../stores/user'

const router = useRouter()
const userStore = useUserStore()

const activeTab = ref('visitor')
const loading = ref(false)

const visitorForm = reactive({
  username: '',
  password: ''
})

const adminForm = reactive({
  username: '',
  password: ''
})

const visitorRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const adminRules = {
  username: [{ required: true, message: '请输入管理员用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入管理员密码', trigger: 'blur' }]
}

const visitorFormRef = ref()
const adminFormRef = ref()

// 硬编码的用户信息
const users = {
  visitor: { username: 'visitor', password: '123456' },
  admin: { username: 'admin', password: 'admin123' }
}

const handleVisitorLogin = () => {
  visitorFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true
      // 模拟登录请求
      setTimeout(() => {
        if (
          visitorForm.username === users.visitor.username &&
          visitorForm.password === users.visitor.password
        ) {
          userStore.login({
            username: visitorForm.username,
            role: 'visitor'
          })
          ElMessage.success('登录成功')
          router.push('/home')
        } else {
          ElMessage.error('用户名或密码错误')
        }
        loading.value = false
      }, 1000)
    }
  })
}

const handleAdminLogin = () => {
  adminFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true
      // 模拟登录请求
      setTimeout(() => {
        if (
          adminForm.username === users.admin.username &&
          adminForm.password === users.admin.password
        ) {
          userStore.login({
            username: adminForm.username,
            role: 'admin'
          })
          ElMessage.success('管理员登录成功')
          router.push('/home')
        } else {
          ElMessage.error('管理员用户名或密码错误')
        }
        loading.value = false
      }, 1000)
    }
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">
        <h2>地铁站点分析系统</h2>
      </div>
      <el-tabs v-model="activeTab" class="login-tabs">
        <el-tab-pane label="游客登录" name="visitor">
          <el-form
            ref="visitorFormRef"
            :model="visitorForm"
            :rules="visitorRules"
            label-width="0"
            size="large"
          >
            <el-form-item prop="username">
              <el-input
                v-model="visitorForm.username"
                placeholder="用户名: visitor"
                prefix-icon="el-icon-user"
              >
                <template #prefix>
                  <el-icon><i class="el-icon-user" /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="visitorForm.password"
                type="password"
                placeholder="密码: 123456"
                prefix-icon="el-icon-lock"
                show-password
              >
                <template #prefix>
                  <el-icon><i class="el-icon-lock" /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                :loading="loading"
                class="login-button"
                @click="handleVisitorLogin"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="管理员登录" name="admin">
          <el-form
            ref="adminFormRef"
            :model="adminForm"
            :rules="adminRules"
            label-width="0"
            size="large"
          >
            <el-form-item prop="username">
              <el-input
                v-model="adminForm.username"
                placeholder="管理员用户名: admin"
                prefix-icon="el-icon-user"
              >
                <template #prefix>
                  <el-icon><i class="el-icon-user" /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="adminForm.password"
                type="password"
                placeholder="管理员密码: admin123"
                prefix-icon="el-icon-lock"
                show-password
              >
                <template #prefix>
                  <el-icon><i class="el-icon-lock" /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                :loading="loading"
                class="login-button"
                @click="handleAdminLogin"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('/src/assets/subway-map-bg.jpg') center center no-repeat, linear-gradient(135deg, #0c4da2, #2c8dd0);
  background-size: cover;
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 24, 64, 0.7);
    z-index: 1;
  }
}

.login-box {
  width: 420px;
  padding: 35px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
  position: relative;
  z-index: 2;
  border-top: 4px solid #0c4da2;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 100px;
    background: linear-gradient(to bottom, rgba(12, 77, 162, 0.05), transparent);
    border-radius: 12px 12px 0 0;
    z-index: -1;
  }
}

.login-title {
  text-align: center;
  margin-bottom: 25px;
  
  h2 {
    color: #0c4da2;
    font-size: 26px;
    font-weight: 600;
    margin: 0;
    position: relative;
    display: inline-block;
    
    &::after {
      content: '';
      position: absolute;
      bottom: -8px;
      left: 50%;
      transform: translateX(-50%);
      width: 60px;
      height: 3px;
      background: linear-gradient(to right, #0c4da2, #2c8dd0);
      border-radius: 3px;
    }
  }
}

.login-tabs {
  margin-top: 20px;
  
  :deep(.el-tabs__nav) {
    width: 100%;
    display: flex;
  }
  
  :deep(.el-tabs__item) {
    flex: 1;
    text-align: center;
    font-size: 16px;
    color: #666;
    
    &.is-active {
      color: #0c4da2;
      font-weight: 600;
    }
  }
  
  :deep(.el-tabs__active-bar) {
    background-color: #0c4da2;
    height: 3px;
  }
}

.login-button {
  width: 100%;
  margin-top: 15px;
  height: 44px;
  font-size: 16px;
  background: linear-gradient(to right, #0c4da2, #2c8dd0);
  border: none;
  
  &:hover, &:focus {
    background: linear-gradient(to right, #0a3d82, #2680c0);
  }
}

:deep(.el-input__wrapper) {
  padding: 0 15px;
  height: 44px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1) inset;
  
  &.is-focus {
    box-shadow: 0 0 0 1px #0c4da2 inset;
  }
}

:deep(.el-input__inner) {
  height: 44px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-icon) {
  color: #0c4da2;
  font-size: 18px;
  margin-right: 8px;
}
</style>